package com.elcom.roaming.common.config;

import org.apache.log4j.xml.DOMConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigModule {
	
	public static final String COLLECTION = "collection";
	public static final String SYNTHESIS = "synthesis";
	
	/**
	 * Directory static config
	 * */
	public static final String HOME_FOLDER = System.getProperty("user.home") + java.io.File.separator;
    public static String CONFIG_FOLDER = "config" + java.io.File.separator;
    public static String MODULE_NAME = COLLECTION;

    public static String MODULE_CONFIG_SMPP_SERVER_FILE = CONFIG_FOLDER + MODULE_NAME + "_smpp_server.cfg";
    public static String MODULE_CONFIG_FILE = CONFIG_FOLDER + "module_" + MODULE_NAME+ "_config.xml";
    public static String MODULE_LOG_CONFIG_FILE = CONFIG_FOLDER + "module_" + MODULE_NAME+ "_config.log4j.xml";
	
    /**
     * Logger name static config
     * */
    public static final String LOG_DEBUG_FLOW = "LOG_DEBUG_FLOW";
	public static final String LOG_DEBUG_DB = "LOG_DEBUG_DB";
	public static final String LOG_DEBUG_SMPP_SERVER = "LOG_DEBUG_SMPP_SERVER";
	public static final String LOG_DEBUG_MONITOR = "LOG_DEBUG_MONITOR";
	public static final String LOG_DEBUG_LB_OUTPUT = "LOG_DEBUG_LB_OUTPUT";
    
    /**
     * DB Config
     * */
	public static String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
	public static String DB_CONNECT_STRING = "jdbc:oracle:thin:@(description=(address=(host=192.168.6.112)(protocol=tcp)(port=1521))(connect_data=(sid=pm3fs)))";
	public static String DB_USERNAME = "chautn";
	public static String DB_PASSWORD = "chautn";
	public static int DB_MAX_CONNECTION = 5;
	public static int DB_QUERY_BATCH = 500;
	public static int DB_MAX_QUEUE_SIZE = 100000;
	public static int DB_INSERT_TIME = 5; // in second
	public static int DB_UPDATE_CACHE_TIME = 5; // in second , only for module synthesis 
	
	/**
	 * Matching config
	 * */
	public static int MATCHING_CLEANING_MAP_TIME = 120; // in second
	public static int MATCHING_MAX_QUEUE_SIZE = 100000;
	public static int MATCHING_MAX_MAP_SIZE = 100000;
	
	/**
	 * Module parameter config
	 * */
	public static int MISS_CALL_TIMEOUT = 90; // in second
	
	/**
	 * Config for only systhesis sub module
	 * */
	public static int MATCHING_MAX_MSRN_MAP_SIZE = 100000;
	
	public static String LB_HOST = "192.168.6.248";
	public static int LB_PORT = 7201;
	public static int LB_CLIENT_MAX_QUEUE_SIZE;
	
	/*********************************************************
	 *Loading* 
	 ********************************************************/
	private static ConfigModule instance = new ConfigModule();
	private Logger logger;
	private ConfigLoader loader;

	public static ConfigModule getInstance() {
		return instance;
	}

	public static boolean isSynthesisModule(){
		return MODULE_NAME.equals(SYNTHESIS);
	}
	
	public void reloadModuleDirectory(String moduleName){
	    MODULE_NAME = moduleName;
	    MODULE_CONFIG_SMPP_SERVER_FILE = CONFIG_FOLDER + MODULE_NAME + "_smpp_server.cfg";
	    MODULE_CONFIG_FILE = CONFIG_FOLDER + "module_" + MODULE_NAME+ "_config.xml";
	    MODULE_LOG_CONFIG_FILE = CONFIG_FOLDER + "module_" + MODULE_NAME+ "_config.log4j.xml";
	}
	
	public void load(){
		DOMConfigurator.configureAndWatch(MODULE_LOG_CONFIG_FILE);
		
		logger = LoggerFactory.getLogger(LOG_DEBUG_FLOW);
		loader = new ConfigLoader(logger, CONFIG_FOLDER, MODULE_CONFIG_FILE);
		reloadConfig();
	}
	
	public void reloadConfig() {
		loader.reloadConfig();

		// load Oracle DB config
		DB_DRIVER = loader.getConfig().getProperty("DB_DRIVER",
				DB_DRIVER, "ORACLE");
		DB_CONNECT_STRING = loader.getConfig().getProperty("DB_CONNECT_STRING",
				DB_CONNECT_STRING, "ORACLE");
		DB_USERNAME = loader.getConfig().getProperty("DB_USERNAME",
				DB_USERNAME, "ORACLE");
		DB_PASSWORD = loader.getConfig().getProperty("DB_PASSWORD",
				DB_PASSWORD, "ORACLE");
		DB_MAX_CONNECTION = loader.getConfig().getIntProperty("DB_MAX_CONNECTION",
				DB_MAX_CONNECTION, "ORACLE");
		DB_QUERY_BATCH = loader.getConfig().getIntProperty("DB_QUERY_BATCH",
				DB_QUERY_BATCH, "ORACLE");
		DB_MAX_QUEUE_SIZE = loader.getConfig().getIntProperty("DB_MAX_QUEUE_SIZE",
				DB_MAX_QUEUE_SIZE, "ORACLE");
		DB_INSERT_TIME = loader.getConfig().getIntProperty("DB_INSERT_TIME",
				DB_INSERT_TIME, "ORACLE");
		if (MODULE_NAME.equals(SYNTHESIS))
			DB_UPDATE_CACHE_TIME = loader.getConfig().getIntProperty("DB_UPDATE_CACHE_TIME",
				DB_UPDATE_CACHE_TIME, "ORACLE");

		// load Matching config
		MATCHING_CLEANING_MAP_TIME = loader.getConfig().getIntProperty("MATCHING_CLEANING_MAP_TIME",
				MATCHING_CLEANING_MAP_TIME, "MATCHING");
		MATCHING_MAX_QUEUE_SIZE = loader.getConfig().getIntProperty("MATCHING_MAX_QUEUE_SIZE",
				MATCHING_MAX_QUEUE_SIZE, "MATCHING");
		MATCHING_MAX_MAP_SIZE = loader.getConfig().getIntProperty("MATCHING_MAX_MAP_SIZE",
				MATCHING_MAX_MAP_SIZE, "MATCHING");

		// load Module config
		MISS_CALL_TIMEOUT = loader.getConfig().getIntProperty("MISS_CALL_TIMEOUT",
				MISS_CALL_TIMEOUT, "MODULE");

		// load for only systhesis module
		if (MODULE_NAME.equals(SYNTHESIS)) {
			MATCHING_MAX_MSRN_MAP_SIZE = loader.getConfig().getIntProperty("MATCHING_MAX_MSRN_MAP_SIZE",
					MATCHING_MAX_MSRN_MAP_SIZE, "SYNTHESIS_MATCHING");
			
			LB_HOST = loader.getConfig().getProperty("LB_HOST",
					LB_HOST, "SYNTHESIS_LB");
			LB_PORT = loader.getConfig().getIntProperty("LB_PORT",
					LB_PORT, "SYNTHESIS_LB");
			LB_CLIENT_MAX_QUEUE_SIZE = loader.getConfig().getIntProperty("LB_CLIENT_MAX_QUEUE_SIZE",
					LB_CLIENT_MAX_QUEUE_SIZE, "SYNTHESIS_LB");
		}
		showLogger();
	}
	
	private void showLogger() {
		logger.info("Module "+ MODULE_NAME + " is reloading initial configuration...");

		logger.info("Reload module configuration success:");
		
		logger.info("\t-ORACLE:");
		logger.info("\t\t+DB_DRIVER = " + DB_DRIVER);
		logger.info("\t\t+DB_CONNECT_STRING = " + DB_CONNECT_STRING);
		logger.info("\t\t+DB_USERNAME = " + DB_USERNAME);
		logger.info("\t\t+DB_PASSWORD = " + DB_PASSWORD);
		logger.info("\t\t+DB_MAX_CONNECTION = " + DB_MAX_CONNECTION);
		logger.info("\t\t+DB_QUERY_BATCH = " + DB_QUERY_BATCH);
		logger.info("\t\t+DB_MAX_QUEUE_SIZE = " + DB_MAX_QUEUE_SIZE);
		logger.info("\t\t+DB_INSERT_TIME = " + DB_INSERT_TIME);
		if (MODULE_NAME.equals(SYNTHESIS)) 
			logger.info("\t\t+DB_UPDATE_CACHE_TIME = " + DB_UPDATE_CACHE_TIME);

		logger.info("\t-MATCHING:");
		logger.info("\t\t+MATCHING_CLEANING_MAP_TIME = " + MATCHING_CLEANING_MAP_TIME);
		logger.info("\t\t+MATCHING_MAX_QUEUE_SIZE = " + MATCHING_MAX_QUEUE_SIZE);
		logger.info("\t\t+MATCHING_MAX_MAP_SIZE = " + MATCHING_MAX_MAP_SIZE);

		logger.info("\t-MODULE:");
		logger.info("\t\t+MISS_CALL_TIMEOUT = " + MISS_CALL_TIMEOUT);
		
		if (MODULE_NAME.equals(SYNTHESIS)) {
			logger.info("\t-SYNTHESIS_MATCHING:");
			logger.info("\t\t+MATCHING_MAX_MSRN_MAP_SIZE = " + MATCHING_MAX_MSRN_MAP_SIZE);

			logger.info("\t-SYNTHESIS_LB:");
			logger.info("\t\t+LB_HOST = " + LB_HOST);
			logger.info("\t\t+LB_PORT = " + LB_PORT);
			logger.info("\t\t+LB_CLIENT_MAX_QUEUE_SIZE = " + LB_CLIENT_MAX_QUEUE_SIZE);
		}

		logger.info("Reload config DONE");

	}
}
